package csce315.group10.androidapp;

import csce315.group10.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class DeletePotentialStoryDialog extends Activity{
	/* private instance variables */
	public CustomApp appState;
	String story_name;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.deletedialog);
        appState = ((CustomApp)getApplicationContext());
        /* get the names of all the potential stories */
    	String story_names[] = new String[appState.GetPStories().size()];
    	for(int i = 0; i < appState.GetPStories().size(); i++)
    	{
    		/* if the user is editor and chief than all stories are available to delete, if not then only stories the user has suggested */
    		if(appState.get_current_user().get_position().equals("Editor And Chief") ||
    				appState.get_current_user().get_name().equals(appState.GetPStories().get(i).get_suggested_by())){
    			story_names[i] = appState.GetPStories().get(i).get_story_name();
    		}
    	}
        Spinner spinner = (Spinner) findViewById(R.id.Delete_item_dropdown);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item,story_names);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new StorySelectedListener());
    }
    
    public void okButtonHandler(View v){
    	Intent data = new Intent();
    	data.putExtra("story_name", story_name);
    	setResult(Activity.RESULT_OK, data);
    	finish();
    }
    /* class that monitors the story selector */
    public class StorySelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
        	story_name = parent.getItemAtPosition(pos).toString();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    }
}